﻿using Sys.APP.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Web;

namespace Sys.APP.Common.SMS
{
    public static class MessageHelper
    {
        #region oldcode
        private static bool IsSendMessage = true;//ConfigData.IsSendMessage;
        private static string UserKey = "59a34d9481cc2f0f0df35a0371826993";//ConfigData.MessageSn; //"59a34d9481cc2f0f0df35a0371826993";//ConfigData.MessageSn;
       
        #endregion

        public static string GetMessageContent(int type)
        {
            if (type == 1)
            {
                return "【驳居】欢迎使用驳居网，您的操作验证码是：{0},请勿泄露，30分钟内操作有效,谢谢使用！"; 
            }
            else if (type == 2)
            {
                return "【驳居】{0}，您已成功预定酒店：{1}，入住时间为：{2}，请准时入住，谢谢！退订回T";
                //return "【驳居】恭喜您，{0}，您的订单已经生成成功，入住时间为{1}，请准时入住酒店！谢谢。退订回T";
            }
            else if (type == 3)
            {
                return "【驳居】{0}，您预定的{1}，入住时间为{2}，已经可以入住了，切勿错过哦，谢谢！退订回T";
            }
            else {
                return "";
            }
        }
        /// <summary>
        /// 发送短信  
        /// </summary>
        /// <param name="phoneNum"></param>
        /// <param name="content"></param>
        /// <param name="type">类型  1验证码  2通知  3 营销</param>
        public static void SendMessage(string phoneNum,string content,int type)
        {
            if (!IsSendMessage)
            {
                return;
            }
            //修改为您的apikey. apikey可在官网（https://www.dingdongcloud.com)登录后获取
            string apikey = UserKey;
            //修改为您要发送的手机号
            string mobile = phoneNum;
            content = HttpUtility.UrlEncode(content, Encoding.UTF8);
            mobile = HttpUtility.UrlEncode(mobile, Encoding.UTF8);
            #region
            //// 查账户信息的http地址
            //string URL_GET_USER_INFO = "https://api.dingdongcloud.com/v1/sms/userinfo";

            //// 验证码短信发送接口的http地址
            //string URL_SEND_YZM = "https://api.dingdongcloud.com/v1/sms/sendyzm";

            //string URL_SEND_YYYZM = "https://api.dingdongcloud.com/v1/sms/sendyyyzm";

            //// 通知短信发送接口的http地址
            //string URL_SEND_TZ = "https://api.dingdongcloud.com/v1/sms/sendtz";

            //// 营销短信发送接口的http地址
            //string URL_SEND_YX = "https://api.dingdongcloud.com/v1/sms/sendyx";
            #endregion

            if (type == 1)//验证码
            {
                string URL_SEND_YZM = "https://api.dingdongcloud.com/v1/sms/sendyzm";
                string data_send_yzm = "apikey=" + apikey + "&mobile=" + mobile + "&content=" + content;
                HttpPost(URL_SEND_YZM, data_send_yzm);

            }
            else if (type == 2) //通知
            {
                string URL_SEND_TZ = "https://api.dingdongcloud.com/v1/sms/sendtz";

                string data_send_tz = "apikey=" + apikey + "&mobile=" + mobile + "&content=" + content;
                HttpPost(URL_SEND_TZ, data_send_tz);
            }
            else
            {
                // 发营销短信调用示例
                // 设置您要发送的内容，短信末尾必须带有“退订回T”
                string URL_SEND_YX = "https://api.dingdongcloud.com/v1/sms/sendyx";
                string data_send_yx = "apikey=" + apikey + "&mobile=" + mobile + "&content=" + content;
                HttpPost(URL_SEND_YX, data_send_yx);
            }
        }
        public static void HttpPost(string Url, string postDataStr)
        {
            byte[] dataArray = Encoding.UTF8.GetBytes(postDataStr);
            // Console.Write(Encoding.UTF8.GetString(dataArray));

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = dataArray.Length;
            //request.CookieContainer = cookie;
            Stream dataStream = request.GetRequestStream();
            dataStream.Write(dataArray, 0, dataArray.Length);
            dataStream.Close();
            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                String res = reader.ReadToEnd();
                reader.Close();
                Console.Write("\nResponse Content:\n" + res + "\n");
            }
            catch (Exception e)
            {
                Console.Write(e.Message + e.ToString());
            }
        }
    }
}